import 'package:chathouse/constant/my_color.dart';
import 'package:chathouse/util/font_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:chathouse/util/base_utils.dart';
import 'package:time_range_picker/time_range_picker.dart';
import 'widget_paras.dart';

typedef TimePickWidget10ItemChange = Function(TimeOfDay start, TimeOfDay end);

//問題類型10 時間選擇
class TimePickWidget10 extends StatefulWidget {
  String? title;
  String placeholder;
  TimeOfDay? start;
  TimeOfDay? end;
  TimePickWidget10ItemChange itemChange;

  bool request;

  String? get getSelectedItemName {
    if (start == null || end == null) return null;
    String str1 = start.toString().split("(").last.replaceAll(")", "");
    String str2 = end.toString().split("(").last.replaceAll(")", "");
    return "$str1-$str2";
  }

  TimePickWidget10(this.title, this.start, this.end, this.itemChange,
      {this.placeholder = "請選擇", this.request = false, Key? key})
      : super(key: key);

  @override
  State<TimePickWidget10> createState() => _TimePickWidget10State();
}

class _TimePickWidget10State extends State<TimePickWidget10> {
  @override
  Widget build(BuildContext context) {
    return Container(
        padding: EdgeInsets.only(left: 26.w, right: 26.w),
        child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
          Row(crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              widget.request
                  ? Text(
                      "*",
                      style: getTextStyle(18.sp, color: Colors.red),
                    )
                  : Container(),
              Expanded(
                  child: Text(
                widget.title ?? "",
                style: getTextStyle(18.sp, color: Colors.black),
              ))
            ],
          ),
          SizedBox(
            height: 12.w,
          ),
          Container(
            height: INPUT_ITEM_HEIGHT.w,
            padding: EdgeInsets.symmetric(horizontal: 12.w),
            decoration: BoxDecoration(
              border: Border.all(color: MyColors.gary_158157157, width: 1.w),
            ),
            child: Row(
              children: [
                Expanded(
                  child: Text(
                    widget.getSelectedItemName ?? widget.placeholder,
                    style: getTextStyle(16.sp,
                        color: widget.getSelectedItemName == null
                            ? MyColors.gary_155155155
                            : Colors.black),
                  ),
                ),
                Icon(
                  Icons.keyboard_arrow_down_rounded,
                  size: 24.w,
                  color: MyColors.gary_112112112,
                )
              ],
            ),
          ),
        ])).setOnClickListener(() {
      showPicker();
    });
  }

  void showPicker() async {
    TimeRange? result = await showTimeRangePicker(
      start: widget.start,
      end: widget.end,
      context: context,
    );
    debugPrint("result " + result.toString());
    if (result != null) {
      setState(() {
        widget.start = result!.startTime;
        widget.end = result!.endTime;
      });
      widget.itemChange(result!.startTime, result!.endTime);
    }
  }
}
